//-------------------------------------------------------------------------
/**
   File:   handle.h

   Descr.: - todo -

   Author: Stefan Leihkauf
   Date:   07.04.2012
*/
//-------------------------------------------------------------------------
#ifndef _WIN_HANDLE_H
#define _WIN_HANDLE_H
#include "common/types/typedef.h"
#include "handle/detail/handle_closer.h"

/* Class handle is a wrapper for Windows handles used by several Windows APIs
   with various valuetypes introducing reference counting for them.

   @param T - Windows handle valuetype (HANDLE, HKEY, HBRUSH, ...)
*/
template <class T>
class handle
{
public:
	typedef typename T value_type;

	static value_type InvalidValue;

private:
	/* the shared pointer doing all the work */
	boost::shared_ptr<void> _handle;

	/* the function passed to the shared_ptr 
	   as destructor 
	*/
	static void close(T& h);

public:
	/// constructor
	handle();
	handle(T h);

	/// reset handle
	void reset();
	void reset(T h);

	/// get handle value
	const T get() const;

	/// check if handle is valid
	bool isValid() const;

	/// conversion to bool
	operator bool () const;

	/// implicit conversion to value_type
	inline operator value_type() const { return (value_type)_handle.get(); }
};
#include "handle.hpp"

#endif //!defined _WIN_HANDLE_H